<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Function.prototype.myCall = function (context) {
        if (typeof this !== "function") {
          throw new Error("error");
        }
        let args = [...arguments].slice(1);
        let result = null;
        context = context || window;
        context.fn = this;
        result = context.fn(...args);
        delete context.fn;
        return result;
      };

      Function.prototype.myApply = function (context) {
        if (typeof this !== "function") {
          throw new Error("error");
        }
        let reuslt = null;
        context = context || window;
        context.fn = this;
        if (arguments[1]) {
          result = context.fn(...arguments[1]);
        } else {
          result = context.fn();
        }
        delete context.fn;
        return result;
      };

      Function.prototype.myBind = function (context, ...args) {
        if (typeof this !== "function") {
          throw new Error("error");
        }
        context = context || window;
        context.fn = this;
        return function Fn(...args2) {
          let newArr = [...args, ...args2];
          const result = context.fn(...newArr);
          delete context.fn;
          return result;
        };
      };

      function sum(num1, num2) {
        console.log(num1, num2, this);
      }

      // 原生的 bind() 方法
      const Fn = sum.bind({ name: "bin" });
      Fn(1,3);

      // 自定义的 binBind() 方法
      const Fn1 = sum.myBind({ name: "bin" });
      Fn1(1,3);

      // function fn() {
      //   console.log([...arguments]);
      //   console.log(...arguments);
      //   console.log(arguments[1]);
      // }

      // fn(1, 2);
    </script>
  </body>
</html>
